/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import java.text.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Period
/*     */ {
/*     */   private ParsedDateTime mStart;
/*     */   private ParsedDateTime mEnd;
/*     */   private ParsedDuration mDuration;
/*     */   private boolean mHasEnd;
/*     */   private static final String FN_START = "dts";
/*     */   private static final String FN_DURATION = "dur";
/*     */   private static final String FN_END = "dte";
/*     */   
/*     */   public Period(ParsedDateTime start, ParsedDateTime end)
/*     */   {
/*  54 */     this.mStart = start;
/*  55 */     this.mEnd = end;
/*  56 */     this.mDuration = this.mEnd.difference(this.mStart);
/*  57 */     this.mHasEnd = true;
/*     */   }
/*     */   
/*     */   public Period(ParsedDateTime start, ParsedDuration dur) {
/*  61 */     this.mStart = start;
/*  62 */     this.mDuration = dur;
/*  63 */     this.mEnd = this.mStart.add(this.mDuration);
/*  64 */     this.mHasEnd = false;
/*     */   }
/*     */   
/*  67 */   public ParsedDateTime getStart() { return this.mStart; }
/*  68 */   public ParsedDateTime getEnd() { return this.mEnd; }
/*  69 */   public ParsedDuration getDuration() { return this.mDuration; }
/*  70 */   public boolean hasEnd() { return this.mHasEnd; }
/*     */   
/*     */   public String toString() {
/*  73 */     StringBuilder sb = new StringBuilder(this.mStart.getDateTimePartString(false));
/*  74 */     sb.append("/");
/*  75 */     if (this.mHasEnd) {
/*  76 */       if (this.mEnd != null) {
/*  77 */         sb.append(this.mEnd.getDateTimePartString(false));
/*     */       }
/*  79 */     } else if (this.mDuration != null) {
/*  80 */       sb.append(this.mDuration.toString());
/*     */     }
/*  82 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public static Period parse(String value, ICalTimeZone tz, TimeZoneMap tzmap) throws ServiceException, ParseException
/*     */   {
/*  87 */     String[] parsed = value.split("\\/", 2);
/*  88 */     if ((parsed.length != 2) || (parsed[0].length() == 0) || (parsed[1].length() == 1))
/*     */     {
/*  90 */       throw ServiceException.INVALID_REQUEST("Invalid PERIOD value \"" + value + "\"", null);
/*     */     }
/*     */     
/*     */ 
/*  94 */     ParsedDateTime startTime = ParsedDateTime.parse(parsed[0], tzmap, tz, tzmap.getLocalTimeZone());
/*     */     
/*     */ 
/*  97 */     char ch = parsed[1].charAt(0);
/*  98 */     if ((ch == 'P') || (ch == '+') || (ch == '-')) {
/*  99 */       ParsedDuration duration = ParsedDuration.parse(parsed[1]);
/* 100 */       return new Period(startTime, duration);
/*     */     }
/* 102 */     ParsedDateTime endTime = ParsedDateTime.parse(parsed[1], tzmap, tz, tzmap.getLocalTimeZone());
/*     */     
/* 104 */     return new Period(startTime, endTime);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Metadata encodeMetadata()
/*     */   {
/* 113 */     Metadata meta = new Metadata();
/* 114 */     meta.put("dts", this.mStart.getDateTimePartString(false));
/* 115 */     if (this.mHasEnd) {
/* 116 */       meta.put("dte", this.mEnd.getDateTimePartString(false));
/*     */     } else {
/* 118 */       meta.put("dur", this.mDuration);
/*     */     }
/* 120 */     return meta;
/*     */   }
/*     */   
/*     */   public static Period decodeMetadata(Metadata meta, ICalTimeZone tz, TimeZoneMap tzmap)
/*     */     throws ServiceException
/*     */   {
/* 126 */     String start = meta.get("dts");
/*     */     ParsedDateTime startTime;
/*     */     try {
/* 129 */       startTime = ParsedDateTime.parse(start, tzmap, tz, tzmap.getLocalTimeZone());
/*     */     } catch (ParseException e) {
/* 131 */       throw ServiceException.INVALID_REQUEST("Invalid PERIOD start time in metadata: " + meta.toString(), e);
/*     */     }
/*     */     
/*     */ 
/* 135 */     String end = meta.get("dte", null);
/* 136 */     if (end != null) {
/*     */       ParsedDateTime endTime;
/*     */       try {
/* 139 */         endTime = ParsedDateTime.parse(end, tzmap, tz, tzmap.getLocalTimeZone());
/*     */       } catch (ParseException e) {
/* 141 */         throw ServiceException.INVALID_REQUEST("Invalid PERIOD end time in metadata: " + meta.toString(), e);
/*     */       }
/*     */       
/* 144 */       return new Period(startTime, endTime);
/*     */     }
/* 146 */     String durStr = meta.get("dur", null);
/* 147 */     if (durStr == null) {
/* 148 */       throw ServiceException.INVALID_REQUEST("PERIOD in metadata missing both end time and duration: " + meta.toString(), null);
/*     */     }
/*     */     
/* 151 */     ParsedDuration duration = ParsedDuration.parse(durStr);
/* 152 */     return new Period(startTime, duration);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/Period.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */